package com.ruoyi.web.controller.scbz;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.time.LocalDate;

@Anonymous
@RestController
@RequestMapping("/scbz/scbz")
public class ScbzController {
    // 天干地支常量
    private static final String[] TIAN_GAN = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
    private static final String[] DI_ZHI = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
    private static final String[] MONTH_ZHI = {"寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥", "子", "丑"};
    private static final String[] WU_XING = {"金","木","水","火","土"};

    @PostMapping("/getResult")
    public StringBuffer getResult(@RequestParam String field101,@RequestParam String field102){

        System.out.println("到了后台！！"+field101+"|"+field102);
        //处理接收到的数据
        String[] ary1 = field101.split("-");
        String[] ary2 = field102.split(":");
        String year = ary1[0];
        String month = ary1[1];
        String day = ary1[2];
        String hour = ary2[0];

        //根据年月日时计算年月日时柱
        //计算年柱
        String nianzhu = getYearGanZhi(Integer.parseInt(year));
        String nianzhu_a = nianzhu.substring(0, 1);
        String nianzhu_b = nianzhu.substring(1,2);
        String nianzhu_a_wuxing = null;
        String nianzhu_b_wuxing = null;
        nianzhu_a_wuxing = getWuxingByTiangan(nianzhu_a);
        nianzhu_b_wuxing = getWuxingByDizhi(nianzhu_b);

        //计算月柱
        String yuezhu = getMonthGanZhi(Integer.parseInt(year),Integer.parseInt(month));
        String yuezhu_a = yuezhu.substring(0,1);
        String yuezhu_b = yuezhu.substring(1,2);
        String yuezhu_a_wuxing = null;
        String yuezhu_b_wuxing = null;
        yuezhu_a_wuxing = getWuxingByTiangan(yuezhu_a);
        yuezhu_b_wuxing = getWuxingByDizhi(yuezhu_b);

        //计算日柱
        String rizhu = getDayGanZhi(LocalDate.parse(field101));
        String rizhu_a = rizhu.substring(0,1);
        String rizhu_b = rizhu.substring(1,2);
        String rizhu_a_wuxing = getWuxingByTiangan(rizhu_a);
        String rizhu_b_wuxing = getWuxingByDizhi(rizhu_b);

        //计算时柱
        String shizhu = getShiZhu(rizhu_a,Integer.parseInt(hour));
        String shizhu_a = shizhu.substring(0,1);
        String shizhu_b = shizhu.substring(1,2);
        String shizhu_a_wuxing = getWuxingByTiangan(shizhu_a);
        String shizhu_b_wuxing = getWuxingByDizhi(shizhu_b);

        //根据年月日时柱计算五行
        //天干：甲乙（木） 丙丁（火） 戊己（土） 庚辛（金） 壬癸（水） 地支：子（水） 丑（土） 寅（木） 卯（木） 辰（土） 已（火） 未（土） 申（金） 酉（金） 戌（土） 亥（水）

        //看喜神

        //输出结果
        String newLine = "<br>";
        StringBuffer res = new StringBuffer("计算结果:");
        res.append("年柱:"+nianzhu_a+"("+nianzhu_a_wuxing+")"+nianzhu_b+"("+nianzhu_b_wuxing+")");
        res.append("月柱:"+yuezhu_a+"("+yuezhu_a_wuxing+")"+yuezhu_b+"("+yuezhu_b_wuxing+")");
        res.append("日柱:"+rizhu_a+"("+rizhu_a_wuxing+")"+rizhu_b+"("+rizhu_b_wuxing+")");
        res.append("时柱:"+shizhu_a+"("+shizhu_a_wuxing+")"+shizhu_b+"("+shizhu_b_wuxing+")");

        String jsonStr = JSON.toJSONString(res);

        return res;
    }

    // 年柱计算
    public static String getYearGanZhi(int year) {
        return TIAN_GAN[(year - 4) % 10] + DI_ZHI[(year - 4) % 12];
    }
    // 月柱计算（需结合节气，此处简化按农历月）
    public static String getMonthGanZhi(int year, int month) {
        String yearGan = TIAN_GAN[(year - 4) % 10];
        int startIndex;
        switch (yearGan) {
            case "甲":
            case "己":
                startIndex = 2; // 丙
                break;
            case "乙":
            case "庚":
                startIndex = 4; // 戊
                break;
            case "丙":
            case "辛":
                startIndex = 6; // 庚
                break;
            case "丁":
            case "壬":
                startIndex = 8; // 壬
                break;
            case "戊":
            case "癸":
                startIndex = 0; // 甲
                break;
            default:
                startIndex = 0;
        }
        return TIAN_GAN[(startIndex + month - 1) % 10] + MONTH_ZHI[month - 1];
    }

    // 日柱计算（简化版，实际需儒略日精确计算）
    public static String getDayGanZhi(LocalDate date) {
        LocalDate baseDate = LocalDate.of(1900, 1, 31); // 基准日：甲戌日
        long days = date.toEpochDay() - baseDate.toEpochDay();
        return TIAN_GAN[(int) (days % 10)] + DI_ZHI[(int) (days % 12)];
    }

    //根据日柱天干及时间计算时柱
    //23:00-01:00子(水) 01:00-03:00丑(土) 03:00-05:00寅(木) 05:00-07:00卯(木) 07:00-09:00辰(土) 09:00-11:00已(火)
    //11:00-13:00午(火) 13:00-15:00未(土) 15:00-17:00申(金) 17:00-19:00酉(金) 19:00-21:00戌(土) 21:00-23:00亥(水)
    public String getShiZhu(String rizhuTiangan,int hour){
        String shiZhu = null;
        if(hour>=23 && hour<=1){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[0]+DI_ZHI[0];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[2]+DI_ZHI[0];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[4]+DI_ZHI[0];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[6]+DI_ZHI[0];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[8]+DI_ZHI[0];
            }
        }else if(hour>=1 && hour<=3){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[1]+DI_ZHI[1];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[3]+DI_ZHI[1];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[5]+DI_ZHI[1];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[7]+DI_ZHI[1];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[9]+DI_ZHI[1];
            }
        }else if(hour>=3 && hour<=5){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[2]+DI_ZHI[2];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[4]+DI_ZHI[2];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[5]+DI_ZHI[2];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[8]+DI_ZHI[2];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[0]+DI_ZHI[2];
            }
        }else if(hour>=5 && hour<=7){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[3]+DI_ZHI[3];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[5]+DI_ZHI[3];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[7]+DI_ZHI[3];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[9]+DI_ZHI[3];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[1]+DI_ZHI[3];
            }
        }else if(hour>=7 && hour<=9){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[4]+DI_ZHI[4];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[6]+DI_ZHI[4];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[8]+DI_ZHI[4];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[0]+DI_ZHI[4];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[2]+DI_ZHI[4];
            }
        }else if(hour>=9 && hour<=11){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[5]+DI_ZHI[5];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[7]+DI_ZHI[5];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[9]+DI_ZHI[5];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[5]+DI_ZHI[5];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[3]+DI_ZHI[5];
            }
        }else if(hour>=11 && hour<=13){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[6]+DI_ZHI[6];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[9]+DI_ZHI[6];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[0]+DI_ZHI[6];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[2]+DI_ZHI[6];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[4]+DI_ZHI[6];
            }
        }else if(hour>=13 && hour<=15){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[7]+DI_ZHI[7];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[9]+DI_ZHI[7];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[1]+DI_ZHI[7];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[3]+DI_ZHI[7];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[5]+DI_ZHI[7];
            }
        }else if(hour>=15 && hour<=17){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[8]+DI_ZHI[8];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[0]+DI_ZHI[8];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[2]+DI_ZHI[8];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[4]+DI_ZHI[8];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[6]+DI_ZHI[8];
            }
        }else if(hour>=17 && hour<=19){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[9]+DI_ZHI[9];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[1]+DI_ZHI[9];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[3]+DI_ZHI[9];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[5]+DI_ZHI[9];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[7]+DI_ZHI[9];
            }
        }else if(hour>=19 && hour<=21){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[0]+DI_ZHI[10];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[2]+DI_ZHI[10];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[4]+DI_ZHI[10];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[6]+DI_ZHI[10];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[8]+DI_ZHI[10];
            }
        }else if(hour>=21 && hour<=23){
            if((rizhuTiangan.equals(TIAN_GAN[0]) || rizhuTiangan.equals(TIAN_GAN[5]))){
                shiZhu = TIAN_GAN[1]+DI_ZHI[11];
            }else if(rizhuTiangan.equals(TIAN_GAN[1]) || rizhuTiangan.equals(TIAN_GAN[6])){
                shiZhu = TIAN_GAN[3]+DI_ZHI[11];
            }else if(rizhuTiangan.equals(TIAN_GAN[2]) || rizhuTiangan.equals(TIAN_GAN[7])){
                shiZhu = TIAN_GAN[5]+DI_ZHI[11];
            }else if(rizhuTiangan.equals(TIAN_GAN[3]) || rizhuTiangan.equals(TIAN_GAN[8])){
                shiZhu = TIAN_GAN[7]+DI_ZHI[11];
            }else if(rizhuTiangan.equals(TIAN_GAN[4]) || rizhuTiangan.equals(TIAN_GAN[9])){
                shiZhu = TIAN_GAN[9]+DI_ZHI[11];
            }
        }else{
            shiZhu = "未知";
        }

        return shiZhu;
    }

    //根据天干获取五行
    public String getWuxingByTiangan(String tianGan){
        String wuxing;
        if(tianGan.equals(TIAN_GAN[0]) || tianGan.equals(TIAN_GAN[1])){
            wuxing = WU_XING[1];
        }else if(tianGan.equals(TIAN_GAN[2]) || tianGan.equals(TIAN_GAN[3])){
            wuxing = WU_XING[3];
        }else if(tianGan.equals(TIAN_GAN[4]) || tianGan.equals(TIAN_GAN[5])){
            wuxing = WU_XING[4];
        }else if(tianGan.equals(TIAN_GAN[6]) || tianGan.equals(TIAN_GAN[7])){
            wuxing = WU_XING[0];
        }else if(tianGan.equals(TIAN_GAN[8]) || tianGan.equals(TIAN_GAN[9])){
            wuxing = WU_XING[2];
        }else {
            wuxing = "未知";
        }
        return wuxing;
    }

    //根据地支获取五行
    public String getWuxingByDizhi(String diZhi){
        String wuxing;
        if(diZhi.equals(DI_ZHI[0]) || diZhi.equals(DI_ZHI[11])){
            wuxing = WU_XING[2];
        }else if(diZhi.equals(DI_ZHI[1]) || diZhi.equals(DI_ZHI[4]) || diZhi.equals(DI_ZHI[7]) || diZhi.equals(DI_ZHI[10])){
            wuxing = WU_XING[4];
        }else if(diZhi.equals(DI_ZHI[2]) || diZhi.equals(DI_ZHI[3])){
            wuxing = WU_XING[1];
        }else if(diZhi.equals(DI_ZHI[5]) || diZhi.equals(DI_ZHI[6])){
            wuxing = WU_XING[3];
        }else if(diZhi.equals(DI_ZHI[8]) || diZhi.equals(DI_ZHI[9])){
            wuxing = WU_XING[0];
        }else {
            wuxing = "未知";
        }
        return wuxing;
    }
}
